package com.yunyao.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunyao.common.web.util.PageResult;
import com.yunyao.common.web.util.StringUtils;
import com.yunyao.common.web.util.YResult;
import com.yunyao.dao.model.BamuPurchaseConsignee;
import com.yunyao.dao.model.BamuSupplyConsignee;
import com.yunyao.dao.service.bamu.IBamuPurchaseConsigneeService;
import com.yunyao.dao.service.bamu.IBamuSupplyApplyService;
import com.yunyao.dao.service.bamu.IBamuSupplyConsigneeService;
import com.yunyao.framework.dto.supplyorder.SupplyConsigneeSaveDto;
import com.yunyao.framework.service.SupplyConsigneeService;
import com.yunyao.framework.vo.supplyorder.SupplyConsigneeVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author chenyejian
 * @date 2024-07-16
 */
@Slf4j
@Service
public class SupplyConsigneeServiceImpl implements SupplyConsigneeService {
    @Autowired
    private IBamuSupplyConsigneeService iBamuSupplyConsigneeService;
    @Override
    public YResult<PageResult<SupplyConsigneeVO>> pageList(String orderId,Long pageIndex,Long pageSize) {
        if (StringUtils.isEmpty(orderId)) {
            return YResult.success(new PageResult<>());
        }
        LambdaQueryWrapper<BamuSupplyConsignee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BamuSupplyConsignee::getPurchaseApplyId,orderId)
                .eq(BamuSupplyConsignee::getIsDeleted,0);
//        List<BamuPurchaseConsignee> list =bamuPurchaseApplyMaterialService.list(queryWrapper);
        IPage<BamuSupplyConsignee> page = new Page<>(pageIndex, pageSize);
        page =iBamuSupplyConsigneeService.page(page, queryWrapper);
        return YResult.success(new PageResult<>(convertDao2VoList(page.getRecords()),pageIndex,pageSize,page.getPages(),page.getTotal()));
    }

    @Override
    public YResult<String> saveOrUpdate(SupplyConsigneeSaveDto saveDto) {
        try {
            Date date = new Date();
            saveDto.setCreatedAt(date);
            saveDto.setUpdatedAt(date);
            BamuSupplyConsignee consignee = convertDto2Dao(saveDto);
            iBamuSupplyConsigneeService.saveOrUpdate(consignee);
            return YResult.success(consignee.getId());
        }catch (Exception e) {
            log.error("保存失败",e);
        }
        return YResult.error("保存失败");
    }

    @Override
    public YResult<String> deleteByIds(String[] ids) {
        try {
            iBamuSupplyConsigneeService.removeByIds(Arrays.asList(ids));
            return YResult.success("删除成功");
        }catch (Exception e){
            log.error("删除失败",e);
        }
        return YResult.error("删除失败");
    }

    private List<SupplyConsigneeVO> convertDao2VoList(List<BamuSupplyConsignee> list){
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<SupplyConsigneeVO> resultList = new ArrayList<>();
        for (BamuSupplyConsignee consignee : list){
            SupplyConsigneeVO projectConsigneeVO = new SupplyConsigneeVO();
            BeanUtils.copyProperties(consignee,projectConsigneeVO);
            resultList.add(projectConsigneeVO);
        }
        return resultList;
    }

    /**
     * saveDto转化为dao
     */
    private BamuSupplyConsignee convertDto2Dao(SupplyConsigneeSaveDto saveDto){
        BamuSupplyConsignee consignee = new BamuSupplyConsignee();
        BeanUtils.copyProperties(saveDto,consignee);
        return consignee;
    }




}
